package week_10.homework;

import java.io.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static week_10.homework.HuffmanTree.breadthFirstTraversal;

public class HuffmanTest {
    //主函数
    public static void main(String[] args) throws IOException {
        //把字符集从文件中读出来，并保存在一个数组characters里面
        File file = new File("D:\\20172328lxy\\20172328lxy\\src\\week_10\\homework\\text.txt");
        Reader reader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(reader);
        String temp = bufferedReader.readLine();

        char characters[] = new char[temp.length()];
        for (int i = 0; i < temp.length(); i++) {
            characters[i] = temp.charAt(i);
        }
        System.out.println("原字符集为：" + Arrays.toString(characters));
        //计算每一个字符出现的频率。用嵌套循环来实现，并把出现的概率存在另一个数组中
        double frequency[] = new double[27];
        int numbers = 0;//空格的个数
        for (int i = 0; i < characters.length; i++) {
            if (characters[i] == ' ') {
                numbers++;
            }
            frequency[26] = (float) numbers / characters.length;
        }
        System.out.println("字符集为");
        for (int j = 97; j <= 122; j++) {
            int number = 0;//给字母计数
            for (int m = 0; m < characters.length; m++) {
                if (characters[m] == (char) j) {
                    number++;
                }
                frequency[j - 97] = (float) number / characters.length;
            }
            System.out.print((char) j + "，");
        }
        System.out.println("空");
        System.out.println("每一个字符对应的概率为（从a到z还有一个空格）" + "\n" + Arrays.toString(frequency));
        double result = 0.0;
        for (int z = 0; z < 27; z++) {
            result += frequency[z];
        }
        System.out.println("总概率之和为" + result);

        //构建出一个列表，每一个字符对应一个出现的频率
        List<Node> nodes = new ArrayList<Node>();
        for (int o = 97; o <= 122; o++) {
            nodes.add(new Node((char) o, frequency[o - 97]));
        }
        nodes.add(new Node(' ', frequency[26]));

        Node root = HuffmanTree.createTree(nodes);

        System.out.println("广度遍历下的哈夫曼树：");
        System.out.println(breadthFirstTraversal(root));

        //对英文文件进行编码，输出一个编码后的文件
        String result1 = "";
        List<Node> temp1 = breadthFirstTraversal(root);

        for (int i = 0; i < characters.length; i++) {
            for (int j = 0; j < temp1.size(); j++) {

                if (characters[i] == temp1.get(j).getData()) {
                    result1 += temp1.get(j).getCodenumber();
                }
            }
        }
        System.out.println("对文件进行编码后的结果为：");
        System.out.println(result1);
        File file2 = new File("D:\\20172328lxy\\20172328lxy\\src\\week_10\\homework\\ciphertext.txt");
        Writer writer = new FileWriter(file2);
        writer.write(result1);
        writer.close();

        //对英文文件进行解码，输出一个解码后的文件

        //将所有具有字符的叶子节点重新保存在一个newlist里面
        List<String> newlist = new ArrayList<>();
        for(int m=0;m < temp1.size();m++)
        {
            if(temp1.get(m).getData()!='无')
                newlist.add(String.valueOf(temp1.get(m).getData()));
        }
        System.out.println("字符："+newlist);

        List<String> newlist1 = new ArrayList<>();
        for(int m=0;m < temp1.size();m++)
        {
            if(temp1.get(m).getData()!='无')
                newlist1.add(String.valueOf(temp1.get(m).getCodenumber()));
        }
        System.out.println("对应编码："+newlist1);



        //先从编完码的文件中读出密文
        FileReader fileReader = new FileReader("D:\\20172328lxy\\20172328lxy\\src\\week_10\\homework\\ciphertext.txt");
        BufferedReader bufferedReader1 = new BufferedReader(fileReader);
        String secretline = bufferedReader1.readLine();

        //将读出的密文存在secretText列表中
        List<String> secretText = new ArrayList<String>();
        for (int i = 0; i < secretline.length(); i++) {
            secretText.add(secretline.charAt(i) + "");
        }

        //解密
        String result2 = "";//最后的解码结果
        String current="";// 临时的保存值
        while(secretText.size()>0) {
            current = current + "" + secretText.get(0);
            secretText.remove(0);
            for (int p = 0; p < newlist1.size(); p++) {
                if (current.equals(newlist1.get(p))) {
                    result2 = result2 + "" + newlist.get(p);
                    current="";
                }

            }
        }

        System.out.println("解码后的结果："+result2);
        File file3 = new File("D:\\20172328lxy\\20172328lxy\\src\\week_10\\homework\\text1.txt");
        Writer writer1 = new FileWriter(file3);
        writer1.write(result2);
        writer.close();
    }
}
